//Main activity for Dr. Odds. Features a SurfaceView where the game is drawn, and two layouts. One layout is for UI elements
//the other holds the SurfaceView and UI layout
package com.drodds.game;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import android.os.Handler;
import android.os.Message;

public class MainActivity extends Activity {
	private static final String TAG = MainActivity.class.getSimpleName();
  
	EngineView gameView; //derived from SurfaceView. Handles game drawing and logic
	FrameLayout frameLayout; //container for gameView and UI
	RelativeLayout uiLayout; //holds UI elements
	GameUI gameUI;
	
	Handler uiHandler;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        frameLayout = new FrameLayout(this);
        uiLayout = new RelativeLayout(this);
        
        uiHandler = new Handler() {
	    	public void handleMessage(Message msg) {
	    		AllUIMessages uiMsg = (AllUIMessages)msg.getData().getSerializable("message");
	    		
	    		switch(uiMsg) {
	    		case GET_DOCK_POSITION:
	    			gameView.setDockBounds(gameUI.getDockBounds());
	    			break;
	    		case RESULTS_READY:
	    			int result = msg.getData().getInt("result");
	    			int resultRange = msg.getData().getInt("resultRange");
	    			gameUI.giveResults(result, resultRange);
	    			break;
	    		case REELS_STOPPED:
	    			gameUI.StoppedSpinning();
	    			break;
	    		}
	    	}
        };
        
        gameView = new EngineView(this, uiHandler);
        gameUI = new GameUI(uiHandler);
        
        //loads the UI into the UI layout
        gameUI.init(uiLayout);
        
        //make UI fill the screen
        RelativeLayout.LayoutParams uiParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        uiLayout.setLayoutParams(uiParams);
           
        frameLayout.addView(gameView);
        frameLayout.addView(uiLayout);
        
        setContentView(frameLayout);
    }
 
    @Override
    protected void onDestroy() {
    	Log.d(TAG, "Destroying...");
    	super.onDestroy();
    }
    
    @Override
    protected void onStop() {
    	Log.d(TAG, "Stopping...");
    	super.onStop();
    }
    
    @Override
    protected void onPause() {
    	Log.d(TAG, "Pausing...");
    	super.onPause();
    }
    
    @Override
    protected void onResume() {
    	Log.d(TAG, "Resuming...");
    	super.onResume();
    }
    
/*
 * ENGINE TO UI
 */
    public void onSurfaceChanged(int newW, int newH) {
    	gameUI.onSurfaceChanged(newW, newH);
    }
  
/*
 * UI TO ENGINE
 */
    public void onStartClicked() {
    	gameView.Spin();
    }
}
